<?php

namespace app\admin\controller;

use think\Db;
use think\Controller;
use service\SmsService;
use service\DataService;
use service\ToolsService;

class Common extends Controller
{
    protected $mod,$role,$system,$nav,$menudata,$cache_model,$categorys,$module,$moduleid;
    public $SystemUser = 'SystemUser';
    public $Area = 'Area';
    public $connection;
    public $SU;
    public function _initialize()
    {
        parent::_initialize();
        define('MODULE_NAME',strtolower(request()->controller()));
        define('ACTION_NAME',strtolower(request()->action()));
        //权限管理

        $this->cache_model=array('Module','Category','Posid','Field','cm');
        foreach($this->cache_model as $r){
            if(!cache($r)){
                savecache($r);
            }
        }
        // $this->categorys = cache('Category');
        $this->module = cache('Module');
        $this->mod = cache('Mod');
        $this->cm = cache('cm');
        
        $this->connection = config('dbx.SU');
        $this->SU = Db::connect($this->connection)->name($this->SystemUser);

        $province = $this->_getPid(['area_parent_id' => 1]);
        $this->assign('province', $province);

        $this->AR = Db::connect($this->connection)->name($this->Area);
        $_areaList = $this->AR->order('area_sort asc, area_id asc')->select();
        $this->areaList = ToolsService::arr2table($_areaList, 'area_id', 'area_parent_id');
        $this->aids = ToolsService::getArrSubIds($this->areaList,session('user.aid'),'area_id','area_parent_id');//当前区域下地区Ids
        $areaList = $this->_getAreaTrees('Area', ['area_open' => 1, 'area_id' => ['in', $this->aids]]);
        $this->assign('areaList', $areaList);
        // halt($this->aids);
    }
    //空操作
    public function _empty(){
        return $this->error('空操作，返回上次访问页面中...');
    }

    //短信发送
    public function _sendSms($phone, $log_msg){        
            $sms = new SmsService();
            return $sms->smsBao($phone, $log_msg);
    }
    
    //地区节点（带上下级）
    public function _getAreaTrees($table = 'Area', $where = ['area_open' => 1]) {
        $_menus = Db::name($table)->where($where)->order('area_sort asc,area_id asc')->select();

        $menus = ToolsService::arr2table($_menus,'area_id','area_parent_id');
        foreach ($menus as $key => &$menu) {

            if (substr_count($menu['path'], '-') > 4) {
                unset($menus[$key]);
                continue;
            }
            if (isset($vo['area_parent_id'])) {
                $current_path = "-{$vo['area_parent_id']}-{$vo['area_id']}";
                if ($vo['area_parent_id'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
                    unset($menus[$key]);
                }
            }
        }
        return $menus;
    }
    
    //站点模板节点（带上下级）
    public function _getTempTrees() {
        $_menus = Db::name($this->tempTreeTable)->select();

        $menus = ToolsService::arr2table($_menus,'id');
        foreach ($menus as $key => &$menu) {

            if (substr_count($menu['path'], '-') > 3) {
                unset($menus[$key]);
                continue;
            }
            // if (isset($vo['area_parent_id'])) {
            //     $current_path = "-{$vo['area_parent_id']}-{$vo['id']}";
            //     if ($vo['area_parent_id'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
            //         unset($menus[$key]);
            //     }
            // }
        }
        return $menus;
    }

    protected function _getPid($where, $area_parent_id = 1) {
        $list = db::connect(config('dbx.SU'))->name('Area')->where($where)->select();
        $option='<option value="0">请选择</option>';
        if(!$where['area_parent_id'])return $option;
        foreach ($list as $k => $v) {
            $selected = $v['area_id'] != $area_parent_id ? "" : ' selected="selected"';
            $option.='<option value="' . $v['area_id'] . '"' . $selected . '>' . $v['area_name'] . '</option>';
        }
        return $option;
    }

    public function _ajaxGetCity(){
        $where = array('area_parent_id'=>input('id'));
        $data = $this->_getPid($where);
        return json($data);
    }
}
